package com.gxa.modules.payment.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.modules.payment.entity.Bill;
import com.gxa.modules.payment.form.BillAddForm;
import com.gxa.modules.payment.form.BillQuerySomeForm;
import com.gxa.modules.payment.form.BillStatusForm;
import com.gxa.modules.payment.form.BillUpdateForm;
import com.gxa.modules.payment.service.BillService;
import com.gxa.modules.payment.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "账单管理接口")
@RestController
@Slf4j
public class BillController {

    @Autowired
    private BillService billService;

//    @ApiOperation("生成 账单编号")
//    @GetMapping("/bill/addNo")
//    public Result addNo(){
//        Result r = Result.failed();
//        try{
//            Integer receiptNo = billService.addNo();
//            r = Result.success(receiptNo);
//        } catch (Exception e){
//            e.printStackTrace();
//        }
//        return r;
//    }

    @ApiOperation(value = "审核状态 下拉列表(0.待缴费,1.缴费成功)")
    @PostMapping(value = "/bill/status")
    public Result<List<BillStatusForm>> selectByStatus(){
        Result<List<BillStatusForm>> listResult = Result.failed("审核状态获取失败");

        try{
            List<BillStatusForm> bills = this.billService.queryByStatus();
            System.out.println("查询记录数————>" + bills);

            Integer size = bills.size();
            Long total = size.longValue();
            System.out.println("total——————>" + total);

            listResult = Result.success(bills,total);
        } catch (Exception e){
            e.printStackTrace();
        }
        return listResult;
    }

    @ApiOperation("账单管理 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value = "当前第几页"),
            @ApiImplicitParam(paramType = "query",name = "limit",value = "每页显示多少数据"),

            @ApiImplicitParam(paramType = "query",name = "status", value = "查询条件 缴费状态(0待缴费，1缴费成功)"),
            @ApiImplicitParam(paramType = "query",name = "service", value = "查询条件 收费项目(11电费,12物业费,13燃气费)"),
            @ApiImplicitParam(paramType = "query",name = "billName", value = "查询条件 账单名称"),
            @ApiImplicitParam(paramType = "query",name = "code", value = "查询条件 账单编号"),
            @ApiImplicitParam(paramType = "query",name = "room", value = "查询条件 房间号"),
            @ApiImplicitParam(paramType = "query",name = "managementName", value = "查询条件 住户姓名"),
    })
    @GetMapping("/bill/list")
    public Result<List<Bill>> queryByPage(@RequestParam @ApiIgnore Map<String,Object> params){
        Result<List<Bill>> r = Result.success();

        String page1 = (String) params.get("page");
        String limit1 = (String) params.get("limit");

        Integer page = Integer.parseInt(page1);
        Integer limit = Integer.parseInt(limit1);

        try{
            PageHelper.startPage(page,limit);
            List<Bill> bills = this.billService.queryByPage(params);
            PageInfo<Bill> pageInfo = new PageInfo<>(bills);

            long total = pageInfo.getTotal();
            return Result.success(bills,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }


    @ApiOperation("账单管理 添加")
    @PostMapping("/bill/add")
    public Result add(@RequestBody BillAddForm billAddForm){
        Result r = Result.failed();

        try{
            this.billService.add(billAddForm);

            return Result.success();
        } catch (Exception e){
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("账单管理 code 查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "code", value = "查询条件 账单编号"),
    })
    @GetMapping("/bill/queryByCode")
    public Result<List<BillQuerySomeForm>> queryByCode(@RequestParam Integer code){
        Result<List<BillQuerySomeForm>> r = Result.success();

        try{
            List<BillQuerySomeForm> bills = this.billService.queryByCode(code);
            PageInfo<BillQuerySomeForm> pageInfo = new PageInfo<>(bills);

            long total = pageInfo.getTotal();
            return Result.success(bills,total);
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("账单管理 修改")
    @PutMapping("/bill/update")
    public Result add(@RequestBody BillUpdateForm billUpdateForm){
        System.out.println(billUpdateForm);
        Result r = Result.failed();

        try{
            this.billService.update(billUpdateForm);

            return Result.success();
        } catch (Exception e){
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("账单管理 删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "code", value = "查询条件 账单编号"),
    })
    @DeleteMapping("/bill/delete")
    public Result delete(@RequestParam Integer code){
        Result r = Result.failed();

        try{
            this.billService.delete(code);

            return Result.success();
        } catch (Exception e){
            e.printStackTrace();
        }
        return r;
    }

    @ApiOperation("收费项目管理 删除多条")
    @DeleteMapping("/bill/ids/delete")
    public Result deletes(@RequestBody List<Integer> ids){
        Result r = Result.success();

        try{
            this.billService.billDeleteIds(ids);
            return Result.success();
        } catch (Exception e){
            r.setCode("1");
            r.setMsg("error");
            e.printStackTrace();
        }
        return r;
    }

}
